package com.funtester.seven.day7;

import com.alibaba.fastjson.JSONObject;
import com.funtester.seven.day4.HttpUtil;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;

import java.util.*;

//import static com.funtester.seven.day4.HttpUtil.*;

public class User extends HttpUtil {

    public String uid;

    public String token;

    public String uname;

    public String pwd;

    public User(String uname, String pwd) {
        this.uname = uname;
        this.pwd = pwd;
        type = uname.equals("admin");
    }

    /**
     * 是否是admin
     */
    public boolean type;

    /**
     * 作业,创建10个用户,创建一个活动(人数限制8个),然后用10个用户去报名这个活动,统计8个成功的订单ID,2个失败的用户ID
     */

    public static void main(String[] args) {
        User admin = new User("admin", "admin");
        admin.login();
        String name = "FunTester";
        for (int i = 0; i < 10; i++) {
            admin.register(name + i, name + i + "@qq.com", "1312045458" + i);
        }
        JSONObject activity = admin.addActivity("测试10人活动", 10, 10);

        String code1 = activity.getString("code");
        Integer code2 = activity.getInteger("code");

        Integer id = activity.getInteger("id");//假代码

//        for (int i = 0; i < 10; i++) {
//            User user = new User(name + i, "123456789");
//            user.login();
//            JSONObject order = user.order(id);
//            Integer code = order.getInteger("code");
//            if (code == 0) {
//                System.out.println(user.uid + "报名失败");
//            } else {
//                System.out.println(user.uid + "报名成功");
//            }
//        }


        List<User> us = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user = new User(name + i, "123456789");
            user.login();
            us.add(user);
        }

        Map<User, Boolean> status = new HashMap<>();

        for (int i = 0; i < us.size(); i++) {
            User user = us.get(i);
            JSONObject order = user.order(id);
            Integer code = order.getInteger("code");
            if (code == 0) {
//                System.out.println(user.uid + "报名失败");
                status.put(user, false);
            } else {
//                System.out.println(user.uid + "报名成功");
                status.put(user, true);
            }
        }

        Set<Map.Entry<User, Boolean>> entries = status.entrySet();
        for (Map.Entry<User, Boolean> entry : entries) {
            if (!entry.getValue()) {
                JSONObject order = entry.getKey().order(id);
                Integer code = order.getInteger("code");
                if (code == 0) {
                    System.out.println(entry.getKey() + "报名失败");
                } else {
                    System.out.println(entry.getKey() + "报名成功");
                }

            }
        }

    }


    /**
     * 预定
     *
     * @param eid
     * @return
     */
    public JSONObject order(int eid) {
        String url = HOST + Order;
        JSONObject params = new JSONObject();
        params.put("eid", eid);
        JSONObject response = postRes(url, params);
        output(response);
        return response;
    }

    /**
     * 获取详情
     *
     * @param id
     * @return
     */
    public JSONObject activityInfo(int id) {
        String url = HOST + ActivytiInfo;
        JSONObject params = new JSONObject();
        params.put("id", id);
        JSONObject response = getRes(url, params);
        output(response);
        return response;
    }

    public JSONObject activityList(int status) {
        String url = HOST + ActivytiList;
        JSONObject params = new JSONObject();
        params.put("status", status);
        JSONObject response = getRes(url, params);
        output(response);
        return response;
    }

    /**
     * 注册新用户
     *
     * @param name
     * @param email
     * @param phone
     * @return
     */
    public JSONObject register(String name, String email, String phone) {
        String url = HOST + Register;
        JSONObject params = new JSONObject();
        params.put("username", name);
        params.put("password", "123456789");
        params.put("phone", phone);
        params.put("email", email);
        JSONObject response = postRes(url, params);
        output(response);
        return response;
    }

    /**
     * 添加活动接口
     *
     * @param title
     * @param limit
     * @param price
     * @return
     */
    public JSONObject addActivity(String title, int limit, int price) {
        String url = HOST + AddActivity;
        JSONObject params = new JSONObject();
        params.put("title", title);
        params.put("limit", limit);
        params.put("address", "北京");
        params.put("time", "2021-12-21 16:00:00");
        params.put("price", price);
        params.put("type", "户外");
        JSONObject response = postRes(url, params);
        output(response);
        return response;
    }

    /**
     * 登录
     *
     * @return
     */
    public JSONObject login() {
        // boolean ? "true" : "false"
        return type ? loginAdmin() : loginUser();
    }

    public JSONObject loginAdmin() {
        String url = HOST + Login;
        JSONObject params = new JSONObject();
        params.put("username", uname);
        params.put("password", base64Encode("123" + pwd));
        JSONObject response = postRes(url, params);
        token = response.getString("token");
        uid = response.getString("uid");
        return response;
    }

    /**
     * 普通用户登录
     *
     * @param username
     * @param password
     * @return
     */
    public JSONObject loginUser() {
        String url = HOST + User_Login;
        JSONObject params = new JSONObject();
        params.put("username", uname);
        params.put("password", base64Encode("123" + pwd));
        JSONObject response = postRes(url, params);
//        output(response);
        token = response.getString("token");
        uid = response.getString("uid");
        return response;
    }

    /**
     * 获取post响应
     *
     * @return
     */
    public JSONObject postRes(String url, JSONObject params) {
        HttpPost post = getPost(url, params);//表单
//        HttpPost post = getPost(url, params.toString());//JSON
        JSONObject response = res(post);
        return response;
    }

    /**
     * 获取GET响应
     *
     * @param url
     * @param params
     * @return
     */
    public JSONObject getRes(String url, JSONObject params) {
        HttpGet get = getGet(url, params);
        JSONObject response = res(get);
        return response;
    }

    /**
     * 根据请求获取响应
     *
     * @param requst
     */
    public JSONObject res(HttpRequestBase requst) {
        boolean contains = requst.getURI().toString().contains(Login);
        if (!contains && uid == null) {
            login();
        }
        requst.addHeader("uid", uid);
        requst.addHeader("key", token);
        //非第一次请求,sleep 3s
        if (key) {
            key = false;
        } else {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        JSONObject response = getResponse(requst);
        return response;
    }

    /**
     * 是否第一次请求
     */
    private static boolean key = true;

    public static String HOST = "http://123.56.99.53:9000";

    public static String Login = "/event/api/admin/";

    public static String User_Login = "/event/api/login/";

    public static String Register = "/event/api/register/";

    public static String AddActivity = "/event/api/add_event/";

    public static String ActivytiList = "/event/api/get_eventlist/";

    public static String ActivytiInfo = "/event/api/get_eventdetail/";

    public static String Order = "/event/api/order/";

}
